<template>
  <div class="y-conter">
    <div class="y-div-1">
      <div class="y-title">
        <span class="y-font-1">参建单位</span>
        <el-button type="submit" style="margin-left:1000px" @click="addCompany()">添加参建单位</el-button>
      </div>
      <!-- 表格部分 -->
      <div class="y-table">
        <el-table
          :border="true"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          ref="multipleTable"
          :data="currList"
          tooltip-effect="dark"
          style="width: 100%, text-align: center "
          @selection-change="handleSelectionChange"
        >
          <el-table-column align="center" type="index" width="50" :index="indexFn" label="序号"></el-table-column>
          <el-table-column
            label="单位名称"
            prop="company_name"
            width="100"
            hight="30px"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="	单位类型"
            prop="buildCompanyType.company_type_name"
            width="120"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="法人代表"
            prop="company_legal"
            align="center"
            width="100"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="统一信用代码" prop="company_code" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column label="	单位地址" prop="company_addr" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column
            label="备注"
            width="100"
            prop="company_remark"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="创建时间"
            width="100"
            prop="company_created"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <!-- <el-table-column
            label="单位类型ID"
            width="100"
            prop="buildCompanyType.company_type_id"
            align="center"
          ></el-table-column> -->
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
              <el-button
                type="text"
                @click="showConfirm(scope.$index, scope.row)"
                v-if="scope.row.buildCompanyType.company_type_id >'1'"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog title="新增参建单位" :visible.sync="dialogFormVisible">
          <el-form :model="form" ref="updataForm">
            <el-row>
              <el-col :span="11">
                <el-form-item label="单位名称:" :label-width="formLabelWidth">
                  <el-input v-model="form.company_name" autocomplete="off" class="padding"> </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="单位类型" :label-width="formLabelWidth">
                  <el-select v-model="form.company_type_id" placeholder="请选择">
                    <el-option
                      v-for="item in finput"
                      :key="item.material_type"
                      :label="item.company_type_name"
                      :value="item.company_type_id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <el-form-item label="单位地址:" :label-width="formLabelWidth">
                  <el-input v-model="form.company_addr" autocomplete="off" class="padding"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="法人代表:" :label-width="formLabelWidth">
                  <el-input v-model="form.company_legal" autocomplete="off" class="padding"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="11">
                <el-form-item label="信用代码:" :label-width="formLabelWidth">
                  <el-input v-model="form.company_code" autocomplete="off" class="padding"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="	备注信息:" :label-width="formLabelWidth">
                  <el-input v-model="form.company_remark" autocomplete="off" class="padding"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="quaoxiao(form)">取 消</el-button>
            <el-button type="primary" @click="SureEdit('updataForm')">确 定</el-button>
          </div>
        </el-dialog>

        <el-dialog title="修改参建单位" :visible.sync="dialogForm">
          <el-form :model="xgForm" ref="updataForm">
            <el-row>
              <el-col :span="11">
                <el-form-item label="单位名称:" :label-width="formLabelWidth">
                  <el-input v-model="xgForm.company_name" autocomplete="off"  class="padding"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="单位类型" :label-width="formLabelWidth">
                  <el-select v-model="xgForm.company_type_id" placeholder="请选择活动区域">
                    <el-option
                      v-for="item in finput"
                      :key="item.material_type"
                      :label="item.company_type_name"
                      :value="item.company_type_id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="11">
                <el-form-item label="单位地址:" :label-width="formLabelWidth">
                  <el-input v-model="xgForm.company_addr" autocomplete="off"  class="padding"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="法人代表:" :label-width="formLabelWidth">
                  <el-input v-model="xgForm.company_legal" autocomplete="off"  class="padding"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="11">
                <el-form-item label="信用代码:" :label-width="formLabelWidth">
                  <el-input v-model="xgForm.company_code" autocomplete="off"  class="padding"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="	备注信息:" :label-width="formLabelWidth">
                  <el-input v-model="xgForm.company_remark" autocomplete="off"  class="padding"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="xgQuXiao(xgForm)">取 消</el-button>
            <el-button type="primary" @click="xgSureEdit('updataForm')">确 定</el-button>
          </div>
        </el-dialog>
      </div>

      <!-- 分页器 -->
      <div class="block">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="tableData.length"
          :page-size="pageSize"
          @current-change="changePage"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import http from "../api";
export default {
  data() {
    return {
      // JinYong:true,
      finput: [],
      // 分页
      tableData: [], //总数居
      currPage: 1, //当前页
      pageSize: 7, //每页数据量
      currList: [], //当前展示的数据数组
      dialogFormVisible: false,
      dialogForm: false,
      form: {
        company_name: "",
        company_legal: "",
        company_code: "",
        company_addr: "",
        company_remark: "",
        company_type_id: "",
      },
      xgForm: {
        company_name: "",
        company_legal: "",
        company_code: "",
        company_addr: "",
        company_remark: "",
        company_type_id: "",
      },
      formLabelWidth: "110px",
    };
  },
  methods: {
    //表格编辑按钮
    handleEdit(index, row) {
      // console.log(index, row);
      this.dialogForm = true;
      this.xgForm = row;
    },
    // 编辑模态框里的提交按钮
    xgSureEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogForm = false;
          http.updBuildCompany(this.xgForm).then((r) => {
            console.log("这是编辑的", r);
            http.getCompanyList({}).then((res) => {
              this.tableData = res.data;
              this.setCurrList(this.currPage);
            });
          });
        } else {
          this.$message({
            showClose: true,
            message: "修改失败",
            type: "error",
          });
          return false;
        }
      });
    },
    // 修改里的取消按钮
    xgQuXiao(xgForm) {
      console.log(xgForm);
      this.dialogForm = false;
      // 点取消后清空
      for (let key in xgForm) {
        xgForm[key] = "";
      }
    },
    // 表格里的删除
    showConfirm(index, row) {
      console.log("删除", row);
      // http.getBuildCompanyTypes  buildCompany/delBuildCompany
      http.delBuildCompany({ company_id: row.company_id }).then((r) => {
        console.log(r);
        http.getCompanyList({}).then((r) => {
          console.log(r);
          this.tableData = r.data;
          console.log("数据", r.data);
          this.setCurrList(1);
        });
        alert(r.msg);
        // this.setCurrList(1);
      });
    },
    // 编辑的确认
    // 添加里的提交按钮
    SureEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false;
          http.addBuildCompany(this.form).then((r) => {
            console.log("这是编辑的", r);
            http.getCompanyList({}).then((res) => {
              this.tableData = res.data;
              this.setCurrList(this.currPage);
            });
          });
        } else {
          this.$message({
            showClose: true,
            message: "修改失败",
            type: "error",
          });
          return false;
        }
      });
    },
    // 编辑里的取消按钮
    quaoxiao(form) {
      console.log(form);
      this.dialogFormVisible = false;
      // 点取消后清空
      for (let key in form) {
        form[key] = "";
      }
    },
    // 添加
    addCompany() {
      this.dialogFormVisible = true;
    },
    // 分页
    setCurrList(index) {
      this.currList = this.tableData.slice(
        (index - 1) * this.pageSize,
        index * this.pageSize
      );
      // console.log('当前页',index);
    },
    //改变当前页
    changePage(val) {
      this.currPage = val;
      this.setCurrList(val);

      // console.log(val);
    },
    handleSelectionChange(val) {
      console.log(val);
      this.newData = val;
      console.log(this.newData);
    },
    // 表格序号
    indexFn(index) {
      index = index + 1 + (this.currPage - 1) * this.pageSize;
      return index;
    },
    //重置按钮
    resetForm(formName) {
      this[formName] = {};
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    http.getCompanyList({}).then((r) => {
      console.log(r);
      this.tableData = r.data;
      console.log("数据", r.data);
      this.setCurrList(1);
    });
    http.getBuildCompanyTypes({}).then((r) => {
      console.log(r);
      this.finput = r.data;
      if (this.finput.company_type_id == 1) {
      }
      console.log("数据", r.data);
      // this.setCurrList(1);
    });
  },
};
</script>
<style  scoped>
.padding >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px !important;
}
.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px;
  /* margin-left: 5px; */
}
.y-conter {
  width: 100%;
  height: 660px;
  background: #fff;
}
.y-div-1 {
  width: 98%;
  height: 580px;
  margin: 0px auto;
}

.y-title {
  width: 100%;
  height: 60px;
  line-height: 60px;
  /* background: red; */

  /* padding-top: 20px; */
}
.y-font-1 {
  font-size: 15px;
  font-weight: bold;
  /* padding-left: 15px; */
}
.el-input >>> .el-input__inner {
  width: 240px !important;
  height: 30px;
  /* margin-left: 10px; */
}
.el-select >>> .el-input__inner {
  width: 240px !important;
  height: 30px;
}
.block {
  margin-left: 85%;
  margin-top: 15px;
}
</style>